package com.cskaoyan.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

/**
 * @author stone
 * @date 2022/05/26 11:41
 */
@Component
public class CountTimeAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        long start = System.currentTimeMillis();

        // 执行委托类方法 → 类似于method.invoke
        Object proceed = methodInvocation.proceed();

        //委托类对象、代理对象、方法、参数 → 想要获得可以通过methodInvocation

        long end = System.currentTimeMillis();
        System.out.println(methodInvocation.getMethod().getName() + "方法的执行时间是：" + (end - start));

        return proceed; //返回值就是代理对象执行方法的返回结果
    }
}
